********************************************************************
* Purpose: Create the following outcomes for mothers 
* 1: Psychologists/psychiatrists consultation
* 2: Test for depression/mental health screening at family GP
* 3: In- and outpatient contact 
********************************************************************
 
do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

********************************************************************************
* Pick out relevant contacts for relevant mothers for all three types of contacts
********************************************************************************

***************************************************************
* appending raw data
***************************************************************

********************************************************************************
*psychologist visits
********************************************************************************
forvalues i = 2010/2019 {
use "$raw\SSSY`i'",clear
keep pnr bruhon honuge kontakt speciale ydtyp SPEC2
keep if inlist(SPEC2, "24", "63")
rename pnr id_mother
merge m:1 id_mother using "$work\moms_no1", keep (2 3) nogen
rename pnr pnrb
rename id_mother pnr
compress
save "$work\psyksssy`i'"	, replace
}

forvalues i = 2020/2020 {
use "$raw\SSSY`i'", clear
keep pnr honuge bruhon kontakt speciale ydtyp SPEC2
tostring SPEC2, replace
tostring speciale, replace
tostring pnr, replace format("%12.0f")
tostring honuge, replace
replace pnr = substr("000000000000",1,12 - length(pnr)) + pnr if pnr != "."
replace pnr = "" if pnr == "."
keep if inlist(SPEC2, "24", "63")
rename pnr id_mother
merge m:1 id_mother using "$work\moms_no1", keep (2 3) nogen
rename pnr pnrb
rename id_mother pnr
compress
save "$work\psyksssy`i'", replace		
}

forvalues i = 2010/2020 {
forvalues j = 1/5 {
use "$work\moms_no`j'",clear
rename pnr pnrb
rename id_mother pnr
merge 1:m pnr using "$work\psyksssy`i'", keep(3) nogen
save "$work\psyksssy`i'merged_cprm`j'",replace
}	
}

forvalues i = 2010/2020 {
use "$work\psyksssy`i'merged_cprm1"
forvalues j = 2/5 {
append using "$work\psyksssy`i'merged_cprm`j'"
}
save "$work\psyksssy`i'merged_cprm", replace
}

use "$work\psyksssy2010merged_cprm",clear
forvalues i = 2011/2020 {
append using "$work\psyksssy`i'merged_cprm", force
}
save "$work\psyksssymerged_cprm", replace
*********************************************************************************

********************************************************************************
* GP contacts with a pychological test
********************************************************************************

forvalues i = 2010/2019 {
use "$raw\SSSY`i'",clear
keep pnr bruhon honuge kontakt speciale ydtyp SPEC2
keep if inlist(speciale,"802147","802148", "804106", "806101","802149")
rename pnr id_mother
merge m:1 id_mother using "$work\moms_no1", keep (2 3) nogen
rename pnr pnrb
rename id_mother pnr
compress
save "$work\conssssy`i'"	, replace
}

forvalues i = 2020/2020 {
use "$raw\SSSY`i'", clear
keep pnr honuge bruhon kontakt speciale ydtyp SPEC2
tostring SPEC2, replace
tostring speciale, replace
tostring pnr, replace format("%12.0f")
tostring honuge, replace
replace pnr = substr("000000000000",1,12 - length(pnr)) + pnr if pnr != "."
replace pnr = "" if pnr == "."
keep if inlist(speciale,"802147","802148", "804106", "806101","802149")
rename pnr id_mother
merge m:1 id_mother using "$work\moms_no1", keep (2 3) nogen
rename pnr pnrb
rename id_mother pnr
compress
save "$work\conssssy`i'", replace		
}

forvalues i = 2010/2020 {
forvalues j = 1/5 {
use "$work\moms_no`j'",clear
rename pnr pnrb
rename id_mother pnr
merge 1:m pnr using "$work\conssssy`i'", keep(3) nogen
save "$work\conssssy`i'merged_cprm`j'",replace
}	
}
forvalues i = 2010/2020 {
use "$work\conssssy`i'merged_cprm1"
forvalues j = 2/5 {
append using "$work\conssssy`i'merged_cprm`j'"
}
save "$work\conssssy`i'merged_cprm", replace
}

use "$work\conssssy2010merged_cprm",clear
forvalues i = 2011/2020 {
append using "$work\conssssy`i'merged_cprm", force
}
save "$work\conssssymerged_cprm", replace

********************************************************************************
********************************************************************************
* psych hospital contacts
********************************************************************************

use "$raw\PSYKADM2018",clear
keep if inrange(substr(C_ADIAG,1,4), "DF00","DF99")
save "$work\PSYKADM",replace

forvalues i = 1/5 {
use "$work\moms_no`i'", clear
rename pnr pnrb
rename id_mother pnr
merge 1:m pnr using "$work\PSYKADM", keep(3) nogen
rename pnr cprm
save "$work\altpsykkontaktmerged_`i'",replace
}
use "$work\altpsykkontaktmerged_1",clear
forvalues i =2/5 {
append using "$work\altpsykkontaktmerged_`i'"	
}
save "$work\altpsykkontakt",replace

forvalues i = 1/5 {
erase "$work\altpsykkontaktmerged_`i'.dta"	
}




********************************************************************************
* Generate outcomes: Monthly indicators for contact related to the relevant kid
* 1) Hospital contacts
********************************************************************************
use "$work\altpsykkontakt",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date == .
destring C_PATTYPE, replace 
keep if C_PATTYPE != 0
gen out = 1

bys cprm mfr_dob date: drop if _n > 1

gen month_at = ceil((date - mfr_dob)/30.25) 
drop if month_at < 0 
drop if month_at > 24

collapse (sum) out, by(cprm mfr_dob month_at)
reshape wide out, i(cprm mfr_dob) j(month_at)

foreach var of varlist out* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
}

********************************************************************************
compress 
save "$work\outmoms_monthly", replace // Outpatetient contacts that start in a given month. 
********************************************************************************

use "$work\altpsykkontakt",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date == .
destring C_PATTYPE, replace 
keep if C_PATTYPE == 0
gen inp = 1

bys cprm mfr_dob date: drop if _n > 1

gen month_at = ceil((date - mfr_dob)/30.25) 
drop if month_at < 0
drop if month_at > 24

collapse (sum) inp, by(cprm mfr_dob month_at)
reshape wide inp, i(cprm mfr_dob) j(month_at)

foreach var of varlist inp* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
}
********************************************************************************
compress 
save "$work\inmoms_monthly", replace // inpatient contact
********************************************************************************


********************************************************************************
* 2. Psychologists/psychiatrists contacts
********************************************************************************
use "$work\psyksssymerged_cprm",clear
drop if honuge == ""
gen psyk = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force


collapse (sum) psyk, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
drop if gp_date<=mfr_dob
gen month_at = ceil((gp_date - mfr_dob)/30.25)

drop if month_at > 24 // 2 yrs
drop if month_at<0

collapse (sum) psyk, by(pnr mfr_dob month_at)
reshape wide psyk, i(pnr mfr_dob) j(month_at)

foreach var of varlist psyk*{
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpmw_`var'
}
rename pnr cprm
compress 
save "$work\psyksssy_monthly",replace

********************************************************************************
* 3. GP mental health related contact
********************************************************************************
use "$work\conssssymerged_cprm",clear
drop if honuge == ""
gen cons = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace

replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) cons, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
drop if gp_date<=mfr_dob

gen month_at = ceil((gp_date - mfr_dob)/30.25) 
drop if month_at < 0 
drop if month_at > 24

collapse (sum) cons, by(pnr mfr_dob month_at)
reshape wide cons, i(pnr mfr_dob) j(month_at)

foreach var of varlist cons* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpmw_`var'
}
rename pnr cprm
compress 
save "$work\conssssy_monthly",replace

********************************************************************************
* Same as above but now yearly contacts insted of monthly
********************************************************************************
use "$work\psyksssymerged_cprm",clear
drop if honuge == ""
gen psyk = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) psyk, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td

drop if gp_date<=mfr_dob // remember to drop pre data...

gen year_at = ceil((gp_date - mfr_dob)/365.25) 
drop if year_at > 2

collapse (sum) psyk, by(pnr mfr_dob year_at)
reshape wide psyk, i(pnr mfr_dob) j(year_at)

foreach var of varlist psyk1 psyk2 {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpm_`var'
}

label var gpm_psyk1 "Psyk in first year of child's life" // labels
label var gpm_psyk2 "Psyk in second year of child's life"
rename pnr cprm
compress 
save "$work\psyksssy_m",replace



use "$work\conssssymerged_cprm",clear
drop if honuge == ""
gen cons = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) cons, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td

drop if gp_date<=mfr_dob 


gen year_at = ceil((gp_date - mfr_dob)/365.25) 
drop if year_at > 2

collapse (sum) cons, by(pnr mfr_dob year_at)
reshape wide cons, i(pnr mfr_dob) j(year_at)

foreach var of varlist cons1 cons2   {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpm_`var'
}

* some lables
label var gpm_cons1 "Cons in first year of child's life"
label var gpm_cons2 "Cons in second year of child's life"
rename pnr cprm
compress 
save "$work\conssssy_m",replace



use "$work\altpsykkontakt",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date > mfr_dob + 730.5 
drop if date < mfr_dob
destring C_PATTYPE, replace 

gen postbirth1y_out = 0
replace postbirth1y_out = 1 if date >= mfr_dob  & date - mfr_dob <= 365.25 & C_PATTYPE != 0
gen postbirth2y_out = 0
replace postbirth2y_out = 1 if date - mfr_dob > 365.25 & date - mfr_dob <= 730.5 & C_PATTYPE != 0 

gen postbirth1y_in = 0
replace postbirth1y_in = 1 if date >= mfr_dob  & date - mfr_dob <= 365.25 & C_PATTYPE == 0
gen postbirth2y_in = 0
replace postbirth2y_in = 1 if date - mfr_dob > 365.25 & date - mfr_dob <= 730.5 & C_PATTYPE == 0

collapse (max) post*, by(mfr_dob cprm)
compress
save "$work\psykkontaktmoms", replace


********************************************************************************
* Same as above but drop contacts for the first two months of life. Used as robustness outcomes
********************************************************************************
use "$work\conssssymerged_cprm",clear
drop if honuge == ""
gen cons = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) cons, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
drop if gp_date<=mfr_dob 

gen year_at = ceil((gp_date - mfr_dob)/365.25) 
gen days_at = ceil((gp_date - mfr_dob)) 
drop if days_at < 30.25*2
drop if year_at > 2

collapse (sum) cons, by(pnr mfr_dob)

foreach var of varlist cons* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpm_`var'_robust
}
rename pnr cprm
compress 
save "$work\conssssy_robust",replace


use "$work\psyksssymerged_cprm",clear
drop if honuge == ""
gen psyk = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) psyk, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
drop if gp_date<=mfr_dob 

gen year_at = ceil((gp_date - mfr_dob)/365.25) 
gen days_at = ceil((gp_date - mfr_dob)) 
drop if days_at < 30.25*2
drop if year_at > 1

collapse (sum) psyk, by(pnr mfr_dob)

foreach var of varlist psyk*{
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpm_`var'_robust
}
rename pnr cprm
compress 
save "$work\psyksssy_robust",replace


use "$work\altpsykkontakt",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date > mfr_dob + 730.5
drop if date < mfr_dob + 2*30.25
destring C_PATTYPE, replace 
gen postbirth2y_robust = 0
replace postbirth2y_robust = 1 if date - mfr_dob <= 730.5

collapse (max) post*, by(mfr_dob cprm)

save "$work\psykkontaktmoms_robust", replace